package com.kefu.chat.service;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.kefu.common.context.KefuContext;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.util.Assert;
import com.kefu.common.util.DivUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kefu.chat.mapper.ChatRptSessionMapper;
import com.kefu.chat.vo.ChatRptSession;
import com.kefu.chat.vo.ChatRptSessionGroupVo;
import com.kefu.common.service.AbstractKefuService;

import javax.annotation.Resource;

@Service
@Transactional
public class ChatRptSessionService  extends AbstractKefuService<ChatRptSession>  {
    @Resource
    protected ChatRptSessionMapper chatRptSessionMapper;

	public ChatRptSession selectGroupList(ChatRptSessionGroupVo search) {
		if (search.getStartTime() == null || search.getEndTime() == null) {
			throw BusinessException.msg("统计时间段不能为空！");
		}
		search.setTenantId(KefuContext.getTid());
		return chatRptSessionMapper.selectGroupList(search);
	}

	public List<Object[]> sessionGroupExport(ChatRptSessionGroupVo search) {
		Assert.isNull(search.getStartTime(), "开始时间不能为空！");
		Assert.isNull(search.getEndTime(), "结束时间不能为空！");
		search.setTenantId(KefuContext.getTid());
		List<Object[]> exportList = new ArrayList<>();
		Object[] title = {msg("总会话数"), msg("总会话时长"), msg("平均会话时长"),
				msg("总消息数"), msg("客户消息数"), msg("客服消息数"), msg("机器人消息数"),
				msg("机器人会话数"), msg("机器人独立接待"), msg("机器人评价会话数"), msg("机器人评价已解决数"), msg("机器人评价未解决数"),
				msg("人工会话数"), msg("有效会话数"), msg("无效会话数"), msg("转接会话数"),
				msg("接入率"), msg("回复率"),
				msg("首次响应时长"), msg("平均响应时长"),
				msg("人工评价会话数"), msg("人工评价已解决数"), msg("人工评价未解决数"),
				msg("客服1星评价"), msg("客服2星评价"), msg("客服3星评价"), msg("客服4星评价"), msg("客服5星评价"),
				msg("排队会话数"), msg("排队接通"), msg("排队离开")};
		exportList.add(title);
		ChatRptSession i = selectGroupList(search);
		if(i!=null){
			Object[] row = {i.getTotalCount(), DateUtil.secondToTime(i.getTotalTime()), DateUtil.secondToTime(divInt(i.getTotalTime(), i.getTotalCount())),
					i.getUserMsgCount() + i.getAdminMsgCount() + i.getRobotMsgCount(), i.getUserMsgCount(), i.getAdminMsgCount(), i.getRobotMsgCount(),
					i.getRobotCount(), i.getRobotOnly(), i.getRobotEvaluate(), i.getRobotSolve(), i.getRobotEvaluate() - i.getRobotSolve(),
					i.getOnlineCount(), i.getUserSendToAdmin(), i.getOnlineCount() - i.getUserSendToAdmin(), i.getTransferCount(),
					DivUtil.percent(i.getOnlineCount(), i.getOnlineCount() + i.getWaitOut()), DivUtil.percent(i.getAdminSendToUser(), i.getOnlineCount()),
					DateUtil.secondToTime(divInt(i.getFirstReplyDuration(), i.getAdminSendToUser())), DateUtil.secondToTime(divInt(i.getAvgReplyDuration(), i.getAdminSendToUser())),
					i.getAdminEvaluate(), i.getAdminSolve(), i.getAdminEvaluate() - i.getAdminSolve(),
					i.getAdminStar1(), i.getAdminStar2(), i.getAdminStar3(), i.getAdminStar4(), i.getAdminStar5(),
					i.getWaitCount(), i.getWaitSuccess(), i.getWaitOut(),};
			exportList.add(row);
		}
		return exportList;
	}

	int divInt(int a, int b) {
		if (b == 0) {
			return 0;
		} else {
			return a / b;
		}
	}

	double divDouble(int a, int b) {
		if (b == 0) {
			return 0;
		} else {
			return a * 1.0 / b;
		}
	}
}
